package tako.wisers.com.android.apis.http;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;

import com.google.android.maps.GeoPoint;

import tako.wisers.com.android.apis.adapter.BitmapUtils;
import tako.wisers.com.android.apis.item.StationItem;
import tako.wisers.com.android.apis.location.Geohash;
import tako.wisers.com.android.apis.parser.StationXMLParser;
import tako.wisers.com.android.apis.util.Configration;
import tako.wisers.com.android.apis.util.Global;

public class StationService {
	private StationXMLParser mXMLPaser;
	private static final String SERVICE_URL = Configration.getInstance()
			.getProperty(Global.STATION_SERVICE_KEY);

	private static final String NAME = "station_name";
	private static final String ADDRESS = "address";
	private static final String CITY = "city";
	private static final String COUNTRY = "country_code";
	private static final String PHONE = "phone";
	private static final String TYPE = "station_type";
	private static final String SUBTYPE = "station_subType";
	private static final String LATITUDE = "lat";
	private static final String LONGITUDE = "lon";
	private static final String URL = "url";
	private static final String PHOTO_URL = "photo_url";
	private static final String OTHER_INFO = "other_info";
	private static final String JSONARRAY = "content";
	private static final String HEADER = "result";

	private final int MILLION = 1000000;
	
	private final int ATTRS_SIZE = 4;

	public List<StationItem> getStationItems(GeoPoint location, int...attrs) {
		mXMLPaser = StationXMLParser.getInstance();
		return getItems(location, attrs);
	}
	
	/**
	 * attrs is  type , subtype , shop , location
	 * @param location
	 * @param attrs
	 * @return
	 */
	private List<StationItem> getItems(GeoPoint location, int...attrs) {
		
		List<StationItem> items = new ArrayList<StationItem>();
		float lat = (float)location.getLatitudeE6() / MILLION;
		float lon = (float)location.getLongitudeE6() / MILLION;
		
		if(attrs.length != ATTRS_SIZE) return null;
		
		String url = String.format(SERVICE_URL, lat, lon, attrs[0] - 1 , attrs[1] , attrs[2] , attrs[3]);
		HttpGeter get = new HttpGeter(url);
		String str = get.getContent();
		Geohash encoder = new Geohash();
		String keyword = encoder.encode(location.getLatitudeE6(), location.getLongitudeE6(), Geohash.STATINBIT);
		parser(attrs[0], items, str, keyword);
		return items;
	}

	private void parser(int Type, List<StationItem> items, String str,
			String keyword) {
		try {
			JSONObject result = new JSONObject(str);
			if (!result.getString(HEADER).equals("succes")) {
				return;
			}
			JSONArray objs = result.getJSONArray(JSONARRAY);
			for (int i = 0; i < objs.length(); i++) {
				JSONObject obj = objs.getJSONObject(i);

				StationItem item = new StationItem(Type);
				item.setTitle(obj.getString(NAME));
				item.setKeyword(keyword);
				item.setOwner(obj.getString(CITY) + " "
						+ obj.getString(ADDRESS));
				item.setPhone(obj.getString(PHONE));
				item.setURL(obj.getString(URL));
				item.setInfo(obj.getString(OTHER_INFO));
				item.setPhotoUrl(obj.getString(PHOTO_URL));
				String photo_url = obj.getString(PHOTO_URL);
				if (photo_url != null && photo_url.length() > 5) {
					Bitmap image = BitmapUtils.loadBitmap(photo_url);
					item.setBitmap(image);
				}
				int latitude = (int) Math.round(obj.getDouble(LATITUDE)
						* MILLION);
				int longitude = (int) Math.round(obj.getDouble(LONGITUDE)
						* MILLION);
				item.setLocation(new GeoPoint(latitude, longitude));
				item.setActivity(Global.STATION_INTENT_ACTIVITY);
				items.add(item);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
